﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TimeReporter_II
{
    public class folderReader
    {
        public static void populateTaskList(ProjectList theList)
        {
            FileInfo[] theFiles = getFilesInFolder(theList.PathToFiles);

            foreach (FileInfo fi in theFiles)
            {
                ProjectObject xx = new ProjectObject(fi);
                xx = projectData(fi);
                theList.AddProject(xx);
            }
        }
        
        
        private static FileInfo[] getFilesInFolder(string pathToFolder)
        {
            DirectoryInfo theFolder = new DirectoryInfo(pathToFolder);
            FileInfo[] theFiles = theFolder.GetFiles("*.csv");
            return theFiles;
        }

        private static ProjectObject projectData(FileInfo fi)
        {
            FileStream aFile = new FileStream(fi.FullName.ToString(), FileMode.Open);
            StreamReader sr = new StreamReader(aFile);
            string strLine;

            //Read past header line
            strLine = sr.ReadLine();
            //Read first data row
            strLine = sr.ReadLine();

            ProjectObject temp = new ProjectObject(fi);

            while (strLine != null)
            {
                temp.taskList.Add(new TaskObject(strLine));

                strLine = sr.ReadLine();
            }

            sr.Close();

            return temp;
        }
    }
}
